package com.example.lenovo.demoa;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ImageReader;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btnHttp;
    private ImageView ivImg;
    private Button btn_net;
    private NetworkImageView net_img;
    private Button btn_imageRequest;
    private Button btn_imageLoader;

    private String imgUrl = "http://img5q.duitang.com/uploads/item/201406/28/20140628142930_snTFK.jpeg";
    private final int Get_IMG = 1001;
    private Handler handler;
    private MyImageCache myImageCache;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestQueue = Volley.newRequestQueue(MainActivity.this);
        myImageCache = new MyImageCache();

        btnHttp = findViewById(R.id.btn_http);
        btn_net = findViewById(R.id.btn_net);
        net_img = findViewById(R.id.net_img);

        btn_imageLoader = findViewById(R.id.btn_imageLooader);
        btn_imageRequest = findViewById(R.id.btn_imageRequest);
        ivImg = findViewById(R.id.iv_img);

        btn_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageLoader imageLoader = new ImageLoader(requestQueue,myImageCache);
                net_img.setDefaultImageResId(R.drawable.moren);
                net_img.setErrorImageResId(R.drawable.error);
                net_img.setImageUrl(imgUrl,imageLoader);
            }
        });
//        btn_imageLoader.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
//                    @Override
//                    public Bitmap getBitmap(String s) {
//                        Log.e(TAG,"getBitmap " + s);
//                        return null;
//                    }
//
//                    @Override
//                    public void putBitmap(String s, Bitmap bitmap) {
//                        Log.e(TAG,"putBitmap " + s);
//                    }
//                });
//                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(ivImg,
//                        R.drawable.moren,R.drawable.error);
//                imageLoader.get(imgUrl,imageListener);
//            }
//        });
        btn_imageLoader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageLoader imageLoader = new ImageLoader(requestQueue,myImageCache);
                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(ivImg,
                        R.drawable.moren,R.drawable.error);
                imageLoader.get(imgUrl,imageListener);
            }
        });

        btn_imageRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageRequest imageRequest = new ImageRequest(imgUrl,
                        new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap bitmap) {
                                ivImg.setImageBitmap(bitmap);
                            }
                        },0,0,Bitmap.Config.ARGB_8888,
                        new Response.ErrorListener(){

                            @Override
                            public void onErrorResponse(VolleyError volleyError) {

                            }
                        });
                requestQueue.add(imageRequest);
            }
        });
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if(message.what == Get_IMG){
                    Bitmap bitmap = (Bitmap) message.obj;
                    ivImg.setImageBitmap(bitmap);
                }
                return false;
            }
        });
        btnHttp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            URL url = new URL(imgUrl);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                            Message message = Message.obtain();
                            message.what = Get_IMG;
                            message.obj = bitmap;
                            handler.sendMessage(message);
                        }catch (MalformedURLException e){
                            e.printStackTrace();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}
